package hos.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import hos.po.P_information;
import hos.service.P_informationSerivceImp;

public class MainView{

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//设置主窗口
		JFrame jf = new JFrame("医院管理系统");
		//设置窗口可见
		jf.setVisible(true);
		//设置窗口大小
		jf.setSize(400,600);
		//设置窗口初始位置（null)则在屏幕中央
		jf.setLocationRelativeTo(null);
		//设置窗口销毁操作：exit为结束人物，dispose为销毁当前窗口
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		//账号获取panel(默认流式布局)
		JPanel accountPanel = new JPanel();
		JLabel accountJLabel = new JLabel("账号：");
		JTextField accounTextField = new JTextField(20);
		accountPanel.add(accountJLabel);
		accountPanel.add(accounTextField);
		
		//密码输入panel
		JPanel pwdJPanel = new JPanel();
		JLabel pwdJLabel = new JLabel("密码：");
		JPasswordField pwdField = new JPasswordField(20);
		pwdJPanel.add(pwdJLabel);
		pwdJPanel.add(pwdField);
		
		//身份选择panel
		JPanel selectJPanel = new JPanel();
		JLabel selectJLabel = new JLabel("选择身份");
		JRadioButton patient = new JRadioButton("患者");
		JRadioButton doctor = new JRadioButton("医生");
		patient.setSelected(true);
		ButtonGroup btnGroup = new ButtonGroup();
		btnGroup.add(patient);
		btnGroup.add(doctor);
		selectJPanel.add(patient);
		selectJPanel.add(doctor);
		
		//登录、注册panel
		JPanel loginJPanel = new JPanel();
		JButton dengluButton = new JButton("登录");
		JButton zhuceButton = new JButton("注册");
		//注册操作
		zhuceButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//产生一个注册界面窗口,患者与医生的注册信息不同
				if(patient.isSelected()) {
					LoginView login = new LoginView(jf);
				}
			}
		});
		//登录操作,区分患者和医生
		dengluButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//患者情况
				if(patient.isSelected()) {
					P_informationSerivceImp p_infoService = new P_informationSerivceImp();
					P_information p_exsit = p_infoService.P_infoLoad(accounTextField.getText(), String.valueOf(pwdField.getPassword()));
					System.out.println(accounTextField.getText()+String.valueOf(pwdField.getPassword()));
					if(p_exsit == null) {
						JOptionPane.showMessageDialog(jf, "请输入正确的账号或密码","提示",JOptionPane.WARNING_MESSAGE);
					}else {
						PatientManageView manager = new PatientManageView(jf,p_exsit);
					}
					System.out.println("患者登录");
				}
				
			}
		}); 
		loginJPanel.add(dengluButton);
		loginJPanel.add(zhuceButton);
		
		//创建垂直盒式容器，将panel面板装入
		Box vBox = Box.createVerticalBox();
		vBox.add(accountPanel);
		vBox.add(pwdJPanel);
		vBox.add(selectJPanel);
		vBox.add(loginJPanel);
		
		//把盒式容器装入窗口
		jf.setContentPane(vBox);
		jf.setVisible(true);
		//调整窗口的大小, 以适合其子组件的首选大小和布局。
		//jf.pack();
		
	}

}
